
export default class FileInfomation {
  constructor(options) {
    this.RawFileName = options.RawFileName;
    this.Source = options.Source;
    this.Param = options.Param;
    this.Tags = options.Tags;
    this.Desc = options.Desc;
    this.Size = options.Size;
    this.Thumbs = options.Thumbs;
    this.Template = options.Template;
    this.ID = options.ID;
  }
  //根据size计算相应的大小表达
  get sizeExpression() {
    if ((this.Size != undefined) || (this.Size != null)) {
      let sizeNum = parseFloat(this.Size);
      if (sizeNum < 1024) {
        return sizeNum + "B";
      } else if (sizeNum < 1024 * 1024) {
        return Math.floor(sizeNum / 1024) + "KB";
      } else if (sizeNum < 1024 * 1024 * 1024) {
        return Math.floor(sizeNum / 1024 / 1024) + "MB";
      } else if (sizeNum < 1024 * 1024 * 1024 * 1024) {
        return Math.floor(sizeNum / 1024 / 1024 / 1024) + "GB";
      }
    }
    else {
      return "";
    }
  }
  get type() {
    let suffix = this.suffix;
    switch (suffix) {
      case "doc": case "docx": case "xls": case "xlsx": case "ppt": case "pptx": case "pdf": return "office";
      // case "pdf":return "office";
      case "png": case "jpeg": case "jpg": return "img";
      default: break
    }
  }
  //获取后缀,小写的
  get suffix() {
    try {
      let list = this.RawFileName.split('.');
      if (list) {
        return list[list.length - 1].toLowerCase();
      }
    } catch (e) {
      console.log("file suffix", e);
      return "";
    }
  }
  static loadList() {
    return new Promise((resolve, reject) => {
      window.api.http_get("/api/prj/fileinformation?prj=" + window.nowPrj.pCode)
        .then(
          res => {
            console.log(res);
            let list = [];
            res.forEach(x => {
              list.push(new FileInfomation(x));
            });
            resolve(list);
          }
        )
    })
  }
  create() {
    return new Promise((resolve, reject) => {
      getApp().globalData.api.http_post(`${getApp().globalData.baseUrl}/api/file/fileinformation/create?prj=${getApp().globalData.nowPrj.GuidID}`,this)
        .then(
          res => {
            // let model = new FileInfomation(res);
            resolve(res);
          }
        )
    })
  }
}
